package com.cat.math;

import java.util.TreeSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/sum-of-largest-prime-substrings/
 * @create 2025/10/13 21:51
 * @since JDK17
 */

public class Solution05 {
    boolean judge(long num) {
        for (long i = 2; i * i <= num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return num >= 2;
    }

    public long sumOfLargestPrimes(String s) {
        char[] arr = s.toCharArray();
        TreeSet<Long> list = new TreeSet<>((a, b) -> Long.compare(b, a));
        int n = arr.length;
        for (int i = 0; i < n; i++) {
            long x = 0;
            for (int j = i; j < n; j++) {
                x = x * 10 + arr[j] - '0';
                if (judge(x)) {
                    list.add(x);
                }
            }
        }
        return list.stream().limit(3).mapToLong(Long::longValue).sum();
    }
}
